#include "texturemanager.h"
#include <gl/gl.h>

void TextureManager::InitTexturing(){
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

int TextureManager::CreateTextureFromPNG(std::vector<unsigned char> image, int width, int height){
	/*img.BitmapData data = bitmap.LockBits(
	  new Rectangle(0, 0, bitmap.Width, bitmap.Height),
	  Img.ImageLockMode.ReadOnly,
	  bitmap.PixelFormat);*/
	  //Img.PixelFormat.Format32bppArgb);

	GLuint tex = 0;

	glGenTextures(1, &tex);

	glBindTexture(GL_TEXTURE_2D, tex);

	/*if (GraphicsManager.EnableMipmap) {
		GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); //Smooth
		GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);
	} else {*/
	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // scale linearly when image bigger than texture
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	//GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest); //Pixely
	//GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
	//}

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (void*)&image[0]);

	//GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
	/*OpenTK.Graphics.Glu.Build2DMipmap(OpenTK.Graphics.TextureTarget.Texture2D,
		(int)PixelInternalFormat.Rgba, data.Width, data.Height, OpenTK.Graphics.PixelFormat.Bgra,
		 OpenTK.Graphics.PixelType.UnsignedByte, data.Scan0);
	bitmap.UnlockBits(data);*/
	return tex;
}
